package com.major.mytomcat;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Response {
    private String protocol = "Http/1.1";
    private Integer code = 200;
    private String msg = "OK";
    private String ContentType = "text/html;charset=utf-8";
    private String ContentLength;

    private Map<String,String>  headers = new HashMap(){{
        put("Content-Type",ContentType);
        }};

    private String data;
    private OutputStream out;

    /**
     * 建立响应的页面
     * @return
     */
    public String buildResponse(){
        StringBuilder builder = new StringBuilder();
        builder.append(this.getProtocol()).append(this.getCode()).append(this.getMsg()).append("\r\n");

        for (Map.Entry<String,String> entry : this.getHeaders().entrySet()){
            builder.append(entry.getKey()).append(": ").append(entry.getValue()).append("\r\n");
        }
        builder.append("\r\n").append(this.getData());

        return builder.toString();

    }

    public void write(){
        try {
            out.write(buildResponse().getBytes());
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void write(String content){
        this.setData(content);
        this.write();
    }


    public Map<String, String> getHeaders() {
        return headers;
    }

    /**
     * 加一个响应头
     */
    public void addHeader(String key,String value){
        this.getHeaders().put(key, value);
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
        this.setContentLength(String.valueOf(data.getBytes().length));
    }

    public String getContentType() {
        return this.getHeaders().get("ContentType");
    }

    public void setContentType(String contentType) {
        this.getHeaders().put("ContentType",contentType);
    }

    public String getContentLength() {
        return this.getHeaders().get("contentLength");
    }

    public void setContentLength(String contentLength) {
        this.getHeaders().put("contentLength", Arrays.toString(this.getData().getBytes()));
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public OutputStream getOutput() {
        return out;
    }

    public void setOutput(OutputStream output) {
        this.out = output;
    }

    public Response() {
    }

    public Response(String protocol, Integer code, String msg) {
        this.protocol = protocol;
        this.code = code;
        this.msg = msg;
    }
}
